package mini.oo;

import java.util.LinkedList;
import java.util.List;

import mini.Env;
import mini.ExpressionList;
import mini.expr.Element;
import mini.expr.Expr;
import mini.statement.Statement;
import mini.util.MiniUtil;

public class MethodInvocationAsStatement extends Statement {

	private String cRef = null;
	private String mName = null;
	private ExpressionList expressionlist = null;
	
	public MethodInvocationAsStatement( String classRef, String methodName, ExpressionList el ) {
		cRef = classRef;
		mName = methodName;
		expressionlist = el;
	}
	
    public void eval(List<Env> listOfEnvs, LinkedList var) {
    	
    	// Find the class reference
    	Element classElement = MiniUtil.lookupSymbol(listOfEnvs, cRef);
    	if( classElement == null ) {
    		System.err.println( "Attempting to make a method call " + cRef + "." + mName + ", but cannot find a reference to " + cRef );
    		System.err.println( "Aborting" );
    		System.exit(0);
    	} else if( !classElement.isClass() ) {
    		System.err.println( "Attempting to make a method call " + cRef + "." + mName + ", but " + cRef + " is not an instantiated class" );
    		System.err.println( "Aborting" );
    		System.exit(0);
    	}
    	
    	// Make the method call
    	MiniClassInstantiation classInstance = classElement.getClassValue();
    	classInstance.callMethod( listOfEnvs, var, mName, expressionlist );
    }
}
